<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showToast, showFailToast } from 'vant';
import axios from 'axios';

const router = useRouter();

const form = ref({
  username: '',
  password: ''
});

const isLoading = ref(false);

const onSubmit = async () => {
  try {
    isLoading.value = true;
    const response = await axios.post('/api/mobile/login', {
      username: form.value.username,
      password: form.value.password
    });

    // localStorage.setItem('token', response.data.token)
    // localStorage.setItem('roleId', response.data.roleId)
    // localStorage.setItem('userid', response.data.username)
    if (response.data.success) {
  const user = response.data.data;
  // 确保roleId存在且是数字类型
  const roleId = user.roleId ? parseInt(user.roleId) : 0;
  
  localStorage.setItem('user', JSON.stringify({
    id: user.userId,
    name: user.username,
    roleId: roleId // 确保是数字
  }));
  
  // 立即跳转，不需要等待Toast关闭
  showToast('登录成功');
  router.push('/main'); 
} else {
      showFailToast(response.data.message || '登录失败');
    }
  } catch (error) {
    showFailToast(error.response?.data?.message || '网络错误');
  } finally {
    isLoading.value = false;
  }
};
</script>

<template>
  <div class="login-container">
    <div class="login-header">
      <van-image
        width="80"
        height="80"
        src="https://fastly.jsdelivr.net/npm/@vant/assets/logo.png"
        class="logo"
      />
      <h1 class="title">系统登录</h1>
    </div>
    
    <van-form @submit="onSubmit" class="login-form">
      <van-cell-group inset>
        <van-field
          v-model="form.username"
          label="用户名"
          placeholder="请输入用户名"
          left-icon="user-o"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="form.password"
          type="password"
          label="密码"
          placeholder="请输入密码"
          left-icon="lock"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      
      <div class="form-actions">
        <van-button 
          round 
          block 
          type="primary" 
          native-type="submit"
          :loading="isLoading"
          loading-text="登录中..."
        >
          登录
        </van-button>
        
        <div class="quick-links">
          <router-link to="/register">注册账号</router-link>
          <router-link to="/forgot-password">忘记密码</router-link>
        </div>
      </div>
    </van-form>
    
    <div class="third-party-login">
      <p class="divider">其他登录方式</p>
      <div class="oauth-buttons">
        <van-icon name="wechat" size="24" color="#07C160" />
        <van-icon name="alipay" size="24" color="#1677FF" />
        <van-icon name="weibo" size="24" color="#E6162D" />
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 0 24px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  color: #333;
  margin: 0;
  font-weight: 500;
}

.login-form {
  width: 100%;
  max-width: 400px;
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.form-actions {
  margin-top: 24px;
}

.quick-links {
  display: flex;
  justify-content: space-between;
  margin-top: 16px;
}

.quick-links a {
  color: #1989fa;
  font-size: 14px;
  text-decoration: none;
}

.third-party-login {
  margin-top: 32px;
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.divider {
  position: relative;
  color: #969799;
  font-size: 14px;
  margin: 16px 0;
}

.divider::before,
.divider::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background-color: #ebedf0;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.oauth-buttons {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-top: 16px;
}

@media (max-width: 480px) {
  .login-form {
    padding: 16px;
  }
  
  .title {
    font-size: 20px;
  }
}
</style>